#include "StateToggleObject.h"
#include "include.h"
StateToggleObject::StateToggleObject( char * name, TextureManager * textureManager, Dictionary * gameState, 
									  bool isActive, bool isGrabbable, bool flipOnce, 
				   					  Vector2D inactivePosition,Vector2D * inactiveVertices, int numInactiveVertices, 
				   					  Vector2D activePosition, Vector2D * activeVertices, int numActiveVertices, float transitionTime,
				   					  char * inactiveTextureName, Vector2D * inactiveTextureVertices, 
				   					  char * activeTextureName, Vector2D * activeTextureVertices)
:Object(textureManager, position,NULL,0,mass,isGrabbable)
{
	this->gameState = gameState;
	this->isActive = isActive;
	this->inactiveVertices = inactiveVertices;
	this->numInactiveVertices = numInactiveVertices;
	this->activeVertices = activeVertices;
	this->numActiveVertices = numActiveVertices;
	this->flipOnce = flipOnce;
	this->transitionTime = transitionTime;
	this->isPinned = true;
	this->activeTextureVertices = activeTextureVertices;
	this->inactiveTextureVertices = inactiveTextureVertices;
	this->inactivePosition = inactivePosition;
	this->activePosition = activePosition;
	currentTransitionTime = 0.0;
	alreadyFlipped = false;
	isTransitioning = false;
	
	gameState->Add(name, (void*)&(this->isActive), BOOL_TYPE_STRING);
	if(this->textureManager)
	{
		if(!this->textureManager->LoadTexture(inactiveTextureName))
		{
			printf("Failed to load texture %s\n", inactiveTextureName);
		}
		else
		{
			inactiveTextureIndex = this->textureManager->TextureIndex(inactiveTextureName);
		}
		if(!this->textureManager->LoadTexture(activeTextureName))
		{
			printf("Failed to load texture %s\n", activeTextureName);
		}
		else
		{
			activeTextureIndex = this->textureManager->TextureIndex(activeTextureName);
		}
	}
	
	if(verticesTransition = ((activeVertices != inactiveVertices) && (numActiveVertices == numInactiveVertices)))
	{
		numVertices = numActiveVertices;
		vertices = (Vector2D*)malloc(sizeof(Vector2D)*numActiveVertices);
		for(int i = 0; i < numActiveVertices; i++)
		{
			if(isActive)
			{
				vertices[i] = activeVertices[i];
			}
			else
			{
				vertices[i] = inactiveVertices[i];
			}
		}
	}
	else
	{
		if(isActive)
		{
			vertices = activeVertices;
			numVertices = numActiveVertices;
		}
		else
		{
			vertices = inactiveVertices;
			numVertices = numInactiveVertices;
		}
	}
	if(isActive)
	{
		position = activePosition;
		material->SetTextureIndex(activeTextureIndex);	
		textureVertices = activeTextureVertices;
	}
	else
	{
		position = inactivePosition;
		material->SetTextureIndex(inactiveTextureIndex);	
		textureVertices = inactiveTextureVertices;
	}
	
	CalculateUpVector();
	CalculateMomentOfInertia();
}
StateToggleObject::~StateToggleObject()
{
	if(activeVertices != inactiveVertices)
	{
		if(inactiveVertices)
		{
			delete(inactiveVertices);
		}
	}
	
	inactiveVertices = NULL;
	if(activeVertices)
	{
		delete(activeVertices);
		activeVertices = NULL;
	}
	
	if(!verticesTransition)
		vertices = NULL;
	
	if(inactiveTextureVertices)
	{
		delete(inactiveTextureVertices);
		inactiveTextureVertices = NULL;
	}
	if(activeTextureVertices)
	{
		delete(activeTextureVertices);
		activeTextureVertices = NULL;
	}
	textureVertices = NULL;
	
}
	
char * StateToggleObject::Action(Vector2D playerPos, Dictionary * gameState)
{
	if(isTransitioning)
		return NULL;
	
	if(position.dist(playerPos) < 10.0)
	{
		if(flipOnce && alreadyFlipped)
		{
				return NULL;
		}
		alreadyFlipped = true;
		isActive = !isActive;
		currentTransitionTime = 0.0;
		
		if(verticesTransition)
		{
			isTransitioning = true;	
		}
		else
		{
			if(isActive)
			{
				position = activePosition;
				vertices = activeVertices;
				numVertices = numActiveVertices;
				material->SetTextureIndex(activeTextureIndex);	
				textureVertices = activeTextureVertices;
			}
			else
			{
				position = inactivePosition;
				vertices = inactiveVertices;
				numVertices = numInactiveVertices;
				material->SetTextureIndex(inactiveTextureIndex);	
				textureVertices = inactiveTextureVertices;
			}
		}
	}
	return NULL;
}
bool StateToggleObject::PreThinkEntryPoint(double elapsedTime)
{
	if(verticesTransition && isTransitioning)
	{
		if(currentTransitionTime < transitionTime)
		{
			/*if(currentTransitionTime == 0.0)
			{
				if(isActive)
					acceleration = (activePosition - inactivePosition)/(elapsedTime * elapsedTime);
				else
					acceleration = (inactivePosition - activePosition)/(elapsedTime * elapsedTime);
			}*/
			for(int i = 0; i < numVertices; i++)
			{
				if(isActive)
				{
					velocity = (activePosition - inactivePosition)/transitionTime;
					position = inactivePosition + (activePosition - inactivePosition) * (currentTransitionTime/transitionTime);
					vertices[i] = inactiveVertices[i] + (activeVertices[i] - inactiveVertices[i]) * (currentTransitionTime/transitionTime);
				}
				else
				{
					velocity = (inactivePosition - activePosition)/transitionTime;
					position = activePosition + (inactivePosition - activePosition) * (currentTransitionTime/transitionTime);
					vertices[i] = activeVertices[i] + (inactiveVertices[i] - activeVertices[i]) * (currentTransitionTime/transitionTime);
				}
			}
			currentTransitionTime += elapsedTime;
		}
		else
		{
			isTransitioning = false;
			currentTransitionTime = 0.0;
			for(int i = 0; i < numVertices; i++)
			{
				if(isActive)
				{
					position = activePosition;
					vertices[i] = activeVertices[i];
					material->SetTextureIndex(activeTextureIndex);	
					textureVertices = activeTextureVertices;
				}
				else
				{
					position = inactivePosition;
					vertices[i] = inactiveVertices[i];
					material->SetTextureIndex(inactiveTextureIndex);		
					textureVertices = inactiveTextureVertices;
				}
			}
		}	
	}
	return false;
}
